import unittest
from selenium import webdriver
import time

from public.login import Mylogin
from selenium.webdriver.common.by import By

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("https://www.saucedemo.com/")
        self.driver.maximize_window()

        time.sleep(2)
        Mylogin(self.driver).login()
        print("starttime:" + time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))
    def tearDown(self):
        print("endtime:" + time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))
        filedir="/Users/liangside/Documents/screen/";
        screen_name = filedir + time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) + '.png'
        self.driver.get_screenshot_as_file(screen_name)
        self.driver.quit()
    def test_jcha(self):
        "检查页面文本包含swag labs"
        actual_text=self.driver.find_element(By.XPATH,'//div[@class="app_logo"]').text
        self.assertEqual(actual_text, "Swag Labs")
        self.assertIn()
    def test_jiar(self):
        "加入购物车"
        # 将首页电包包加入购物车
        self.driver.find_element(By.XPATH,'//*[@id="add-to-cart-sauce-labs-backpack"]').click()
        # 点击购物车
        self.driver.find_element(By.XPATH, '//*[@id="shopping_cart_container"]/a').click()
        time.sleep(4)
        bbname=self.driver.find_element(By.XPATH,'//*[@id="item_4_title_link"]/div').text
        print(bbname)
        # 检查包包是否加入购物车
        self.assertEqual(bbname,"Sauce Labs Backpack")
        # 检查页面含有remove
        rname=self.driver.find_element(By.XPATH,'//*[@id="remove-sauce-labs-backpack"]').text
        self.assertEqual(rname,"Remove")

        # 移出购物车
        self.driver.find_element(By.XPATH, '//*[@id="remove-sauce-labs-backpack"]').click()
        time.sleep(3)
        # 检查bb已经移除购物车
        yichu=self.driver.find_elements(By.XPATH,'//*[@id="item_4_title_link"]/div')
        self.assertEqual(len(yichu),0)

        self.driver.find_element(By.XPATH,'//*[@id="continue-shopping"]').click()


if __name__ == '__main__':
    unittest.main()
